package com.im.service.friendship.controller;

import com.im.common.model.RestResponse;
import com.im.common.model.SyncReq;
import com.im.common.model.SyncResp;
import com.im.service.friendship.entity.FriendShip;
import com.im.service.friendship.model.req.*;
import com.im.service.friendship.model.resp.CheckFriendShipResp;
import com.im.service.friendship.model.resp.ImportFriendShipResp;
import com.im.service.friendship.service.ImFriendService;
import com.im.service.user.entity.ImUserDataEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "好友关系控制器")
@RestController
@RequestMapping("/v1/friendship")
public class ImFriendShipController {

    @Autowired
    ImFriendService imFriendShipService;

    @ApiOperation("导入朋友关系")
    @RequestMapping(value = "/importFriendShip", method = RequestMethod.POST)
    public RestResponse importFriendShip(@RequestBody @Validated ImporFriendShipReq req, Integer appId) {
        req.setAppId(appId);
        ImportFriendShipResp resp = imFriendShipService.importFriendShip(req);
        return RestResponse.success(req);
    }

    @ApiOperation("添加朋友关系")
    @RequestMapping(value = "/addFriend", method = RequestMethod.POST)
    public RestResponse addFriend(@RequestBody @Validated AddFriendReq req, Integer appId) {
        req.setAppId(appId);
        imFriendShipService.addFriend(req);
        return RestResponse.success();
    }

    @ApiOperation("更新好友关系")
    @RequestMapping(value = "/updateFriend", method = RequestMethod.POST)
    public RestResponse updateFriend(@RequestBody @Validated UpdateFriendReq req, Integer appId) {
        req.setAppId(appId);
        imFriendShipService.updateFriend(req);
        return RestResponse.success();
    }

    @ApiOperation("删除好友关系")
    @RequestMapping(value = "/deleteFriend", method = RequestMethod.POST)
    public RestResponse deleteFriend(@RequestBody @Validated DeleteFriendReq req, Integer appId) {
        req.setAppId(appId);
        imFriendShipService.deleteFriend(req);
        return RestResponse.success();
    }

    @ApiOperation("删除所有好友")
    @RequestMapping(value = "/deleteAllFriend", method = RequestMethod.POST)
    public RestResponse deleteAllFriend(@RequestBody @Validated DeleteFriendReq req, Integer appId) {
        req.setAppId(appId);
        imFriendShipService.deleteAllFriend(req);
        return RestResponse.success();
    }

    @ApiOperation("获取所有的好友关系")
    @RequestMapping(value = "/getAllFriendShip", method = RequestMethod.POST)
    public RestResponse getAllFriendShip(@RequestBody @Validated GetAllFriendShipReq req, Integer appId) {
        req.setAppId(appId);
        List<FriendShip> allFriendShip = imFriendShipService.getAllFriendShip(req);
        return RestResponse.success(allFriendShip);
    }

    @ApiOperation("获取关系")
    @RequestMapping(value = "/getRelation", method = RequestMethod.POST)
    public RestResponse getRelation(@RequestBody @Validated GetRelationReq req, Integer appId) {
        req.setAppId(appId);
        FriendShip relation = imFriendShipService.getRelation(req);
        return RestResponse.success(relation);
    }

    @ApiOperation("检查好友")
    @RequestMapping(value = "/checkFriend", method = RequestMethod.POST)
    public RestResponse checkFriend(@RequestBody @Validated CheckFriendShipReq req, Integer appId) {
        req.setAppId(appId);
        List<CheckFriendShipResp> checkFriendShipResps = imFriendShipService.checkFriendship(req);
        return RestResponse.success(checkFriendShipResps);
    }

    @ApiOperation("拉黑好友")
    @RequestMapping(value = "/addBlack", method = RequestMethod.POST)
    public RestResponse addBlack(@RequestBody @Validated AddFriendShipBlackReq req, Integer appId) {
        req.setAppId(appId);
        ImUserDataEntity userData = imFriendShipService.addBlack(req);
        return RestResponse.success(userData);
    }

    @ApiOperation("移除拉黑")
    @RequestMapping(value = "/deleteBlack", method = RequestMethod.POST)
    public RestResponse deleteBlack(@RequestBody @Validated DeleteBlackReq req, Integer appId) {
        req.setAppId(appId);
        imFriendShipService.deleteBlack(req);
        return RestResponse.success();
    }

    @ApiOperation("检查拉黑")
    @RequestMapping(value = "/checkBlck", method = RequestMethod.POST)
    public RestResponse checkBlck(@RequestBody @Validated CheckFriendShipReq req, Integer appId) {
        req.setAppId(appId);
        List<CheckFriendShipResp> checkFriendShipResps = imFriendShipService.checkBlck(req);
        return RestResponse.success(checkFriendShipResps);
    }

    @ApiOperation("同步好友列表")
    @RequestMapping(value = "/syncFriendshipList", method = RequestMethod.POST)
    public RestResponse syncFriendshipList(@RequestBody @Validated SyncReq req, Integer appId) {
        req.setAppId(appId);
        SyncResp<FriendShip> friendShipSyncResp = imFriendShipService.syncFriendshipList(req);
        return RestResponse.success(friendShipSyncResp);
    }
}
